﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Repository;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.OpenXmlFormats.Dml;

namespace DFGK_WMS.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class epcController : ControllerBase
    {
        private readonly IInterfaceLogRepository _interfaceLogRepo;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IReceivingOrderDetailService _receivingOrderDetailService;
        public epcController(IInterfaceLogRepository interfaceLogRepo, IUnitOfWork unitOfWork, IReceivingOrderDetailService receivingOrderDetailService)
        {
            _interfaceLogRepo = interfaceLogRepo;
            _unitOfWork = unitOfWork;
            _receivingOrderDetailService = receivingOrderDetailService;
        }

        [HttpPost, Route("post")]
        public void Post(EPCInputModel model)
        {
            var interfaceLog = new InterfaceLog
            {
                InParam = JsonConvert.SerializeObject(model),
                CreateDate = DateTime.Now,
                CallWayStr = "Post",
                CallWay = 2,
                Name = "EPC入库上报",
                Url = "api/epc/post",

            };

            try
            {
                bool result = _receivingOrderDetailService.EPCGetDataIn(model, out string msg);
                interfaceLog.OutParam = msg;
                interfaceLog.ExecDate = DateTime.Now;
                interfaceLog.Status = 0;
            }
            catch (Exception ex)
            {
                interfaceLog.Status = 3;
                interfaceLog.ErrorMsg = ex.Message;
            }
            finally
            {
                interfaceLog.Status = 1;
                _interfaceLogRepo.AddEntity(interfaceLog);
                _unitOfWork.SaveChangesEntity();
            }
        }


        [HttpGet, Route("get")]
        public List<InterfaceLog> GetList(int pageIndex = 1, int pageSize = 50)
        {
            return _interfaceLogRepo.GetEntityAll().Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id).ToList();
        }
      

    }
}
